package com.company.phonebook;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class PhoneBook extends Activity implements OnTouchListener{
    
	float xAtDown;
	float xAtUp;
	
	ViewFlipper mainFlipper;
	LinearLayout bottom;
	
	TextView viewIndicator1;
	TextView viewIndicator2;
	TextView viewIndicator3;
	
	static final int FIRST_VIEW = 0;
	static final int SECOND_VIEW = 1;
	static final int THIRD_VIEW = 2;
	
	ScrollView scrollView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);     
        
        mainFlipper = (ViewFlipper) findViewById(R.id.main_flipper);         
        bottom = (LinearLayout) findViewById(R.id.bottom);
        bottom.setOnTouchListener(this);
        
        viewIndicator1 = (TextView) findViewById(R.id.view_indicator_1);        
        viewIndicator2 = (TextView) findViewById(R.id.view_indicator_2);
        viewIndicator3 = (TextView) findViewById(R.id.view_indicator_3);
        
        scrollView = (ScrollView) findViewById(R.id.scroll_view);
        scrollView.setVerticalFadingEdgeEnabled(false);
        scrollToBottom();
        scrollView.setVisibility(View.VISIBLE);
        
        
        ImageButton addButton = (ImageButton) findViewById(R.id.add_button);        
        addButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent i = new Intent().setClass(PhoneBook.this, addMenu.class);
				startActivity(i);				
			}
		});                        
        
        ImageButton searchButton = (ImageButton) findViewById(R.id.search_button);         
        searchButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent i = new Intent().setClass(PhoneBook.this, searchMenu.class);
				startActivity(i);
			}
		});
        
        ImageButton bumpButton = (ImageButton) findViewById(R.id.bump_button);
        bumpButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent i = new Intent().setClass(PhoneBook.this, bumpMenu.class);
				startActivity(i);
			}
		});
        
        ImageButton recentButton = (ImageButton) findViewById(R.id.recent_button);
        recentButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent i = new Intent().setClass(PhoneBook.this, recentMenu.class);
				startActivity(i);
			}
		});
    }
    
    public void scrollToBottom(){
    	scrollView.post(new Runnable() {
        	public void run() {
        		scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        		}           
        	});    	
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuInflater menuInflater = getMenuInflater();
    	menuInflater.inflate(R.menu.main_context_menu, menu);
    	return true;
    }
    
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(v != bottom)
			return false;
		if(event.getAction() == MotionEvent.ACTION_DOWN)
			xAtDown = event.getX();
		else if(event.getAction() == MotionEvent.ACTION_UP){
			xAtUp = event.getX();
			
			if(xAtUp < xAtDown){				
				mainFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
				mainFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
				mainFlipper.showNext();				
				scrollToBottom();
				showViewIndicator(mainFlipper.getDisplayedChild());				
			}
			else if(xAtUp > xAtDown){
				mainFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
				mainFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
				mainFlipper.showPrevious();
				scrollToBottom();
				showViewIndicator(mainFlipper.getDisplayedChild());				
			}
				
		}
		return true;
	}
	public void showViewIndicator(int currentView){
		switch(currentView){
			case FIRST_VIEW:
				viewIndicator1.setBackgroundResource(R.color.bright_grey);
				viewIndicator2.setBackgroundResource(R.color.black_background);
				viewIndicator3.setBackgroundResource(R.color.black_background);
				break;
			case SECOND_VIEW:
				viewIndicator1.setBackgroundResource(R.color.black_background);
				viewIndicator2.setBackgroundResource(R.color.bright_grey);
				viewIndicator3.setBackgroundResource(R.color.black_background);
				break;
			case THIRD_VIEW:
				viewIndicator1.setBackgroundResource(R.color.black_background);
				viewIndicator2.setBackgroundResource(R.color.black_background);
				viewIndicator3.setBackgroundResource(R.color.bright_grey);
				break;
		}					
	}
}